<?php

namespace App\Http\Controllers;

use App\Models\DeptModel;
use App\Services\DeptService;
use Illuminate\Http\Request;

/**
 * 部门管理-控制器
 * @author
 * @since 2020/11/11
 * Class DeptController
 * @package App\Http\Controllers
 */
class DeptController extends Backend
{
    /**
     * 构造函数
     * @author aaron
     * @since 2020/11/11
     * DeptController constructor.
     */
    public function __construct()
    {
        parent::__construct();
        $this->service = new DeptService();
    }

    /**
     * @OA\Get(
     *     path="/dept/index",
     *     tags={"Dept"},
     *     summary="获取部门数据",
     *     description="获取部门数据",
     *     operationId="Dept_index",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="name", in="query", description="部门名称", required=false, @OA\Schema(default="", type="string")),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function index()
    {
        $result = parent::index();
        $data = $result['data'] ?? [];
        if (!$data) {
            return $result;
        }
        $ids = collect($data)->pluck('id')->toArray();

        $manages = DeptModel::query()
            ->select(['dept.id as dept_id', 'user.username'])
            ->leftJoin('user', 'dept.manager_id', '=', 'user.id')
            ->whereIn('dept.id', $ids)
            ->get()
            ->keyBy('dept_id')
            ->toArray();
        $newData = [];
        foreach ($data as $item) {
            $item['manager_name'] = $manages[$item['id']]['username'] ?? '';
            $newData[] = $item;
        }
        $result['data'] = $newData;
        return $result;
    }

    /**
     * @OA\Get(
     *     path="/dept/{id}/users",
     *     tags={"Dept"},
     *     summary="获取部门及所有子部门人员",
     *     description="获取部门及所有子部门人员",
     *     operationId="dept_users",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="id",in="path",description="部门ID",required=true,@OA\Schema(default="",type="integer")),
     *     @OA\Parameter(name="page", in="query", description="当前页数", required=false, @OA\Schema(default="", type="integer")),
     *     @OA\Parameter(name="limit", in="query", description="每页数量(默认10)", required=false, @OA\Schema(default=10,type="integer")),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function users(Request $request, $id)
    {
        try {
            $users = $this->service->getAllUsers($id);
            return list_message('获取成功', true, $users);
        } catch (\Exception $e) {
            return message($e->getMessage(), false, null);
        }
    }

    /**
     * @OA\Get(
     *     path="/dept/engineers",
     *     tags={"Dept"},
     *     summary="按部门获取工程师",
     *     description="按部门获取工程师",
     *     operationId="dept_engineers",
     *     security={{"bearerAuth":{}}},
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function engineers(Request $request)
    {
        return message('获取成功', true, $this->service->getEngineers());
    }

    /**
     * 获取部门列表
     * @return mixed
     * @since 2021/5/26
     * @author aaron
     */
    public function getDeptList()
    {
        $result = $this->service->getDeptList();
        return $result;
    }
}
